
$(document).ready(function(){
    // render
    loadData();
    // event
    bindEventEdit();
});

function loadData() {
    var _maLoaiDMSP = ISS.SESSION.getSession('session_maLoaiDMSP');

    var productlist = {};
    productlist.maLoaiDMSP = _maLoaiDMSP;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/editListProduct',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(productlist),
        success: function (data) {
            var responseContent = data.responseObject;
            loadDataComplete(responseContent);
        }
    });

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/getAllProductCategory',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'GET',
        dataType: 'json',
        success: function (data) {
            var responseContent = data.responseObject;
            mapAllListCategoryToView(responseContent);
        }
    });

}

function loadDataComplete(list){
    ISS.DOM.setVal('#AdListProduct_Code', list.maLoaiDMSP);
    ISS.DOM.setVal('#AdListProduct_Name', list.tenLoaiDMSP);
    ISS.DOM.setVal('#AdListCategory_Hidden', list.loaiDanhMuc.maLoai);
}

function bindEventEdit(){
    ISS.EVENT.bindEvent('#AdListProduct_Modified1', 'click', function () {
        sendInfoEdit();
    });
}

function sendInfoEdit() {
    var list = {};
    list.maLoaiDMSP = ISS.DOM.getVal('#AdListProduct_Code');
    list.tenLoaiDMSP = ISS.DOM.getVal('#AdListProduct_Name');

    var category = {};
    category.maLoai = ISS.DOM.getVal('#AdListCategory_Option');
    list.loaiDanhMuc = category;

    ISS.BACKEND.request({
        url: ISS.SESSION.getSession('contextPath') + '/service/admin/registerProductListEdit',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify(list),
        success: function (data) {
            var responseContent = data.responseObject;
            sendInfoEditComplete(responseContent);
        }
    });
}

function sendInfoEditComplete(list) {
    if(list) {
        alert('Cập nhật thành công');
        ISS.WINDOW.location('/jsp/admin/ad-product-listproduct-common.jsp');
    }
    else{
        alert('Cập nhật không thành công');
    }
}

function mapAllListCategoryToView(productCategory) {
    var listLDM = ISS.DOM.getVal('#AdListCategory_Hidden');
    var n = '';
    var html = '<option value="default_value">...</option>';
    for(var i=0; i<productCategory.length; i++) {
        if (productCategory[i].maLoai == listLDM){
            n = 'selected="selected"';
        }
        else{
            n = '';
        }
        html += '<option value="' + productCategory[i].maLoai + '" ' + n + '  >' + productCategory[i].tenLoai + '</option>';
    }
    ISS.DOM.html('#AdListCategory_Option', html);
}
